#include <string.h>

#include "check.h"
#include "common_interface.h"
#include "check_interface.h"
#include "common_errno.h"

static char decode[] = "test";
static char encode[] = "dGVzdA==";

static int decode_len = 4;
static int encode_len = 8;

START_TEST(test_ok) {
    char ret_decode[256] = { 0 };
    char ret_encode[256] = { 0 };
    int len_de = 0, len_en = 0;
    ck_assert_int_eq(base64_encode(ret_encode, &len_en, sizeof(ret_encode), decode, decode_len), COM_OK);
    ck_assert_int_eq(base64_decode(ret_decode, &len_de, sizeof(ret_decode), encode, encode_len), COM_OK);
    ck_assert_str_eq(ret_encode, encode);
    ck_assert_str_eq(ret_decode, decode);
}
END_TEST

START_TEST(test_input) {
    char ret[256] = { 0 };
    int len = 0;

    ck_assert_int_eq(base64_encode(NULL, &len, sizeof(ret), decode, decode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_encode(ret, NULL, sizeof(ret), decode, decode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_encode(ret, &len, 0, decode, decode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_encode(ret, &len, -1, decode, decode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_encode(ret, &len, sizeof(ret), NULL, decode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_encode(ret, &len, sizeof(ret), decode, 0), COM_BAD_INPUT);
    ck_assert_int_eq(base64_encode(ret, &len, sizeof(ret), decode, -1), COM_BAD_INPUT);

    // ck_assert_int_eq(base64_decode(ret, &len, sizeof(ret), encode, encode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_decode(NULL, &len, sizeof(ret), encode, encode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_decode(ret, NULL, sizeof(ret), encode, encode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_decode(ret, &len, 0, encode, encode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_decode(ret, &len, -1, encode, encode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_decode(ret, &len, sizeof(ret), NULL, encode_len), COM_BAD_INPUT);
    ck_assert_int_eq(base64_decode(ret, &len, sizeof(ret), encode, 0), COM_BAD_INPUT);
    ck_assert_int_eq(base64_decode(ret, &len, sizeof(ret), encode, -1), COM_BAD_INPUT);

}
END_TEST

START_TEST(test_small_buffer) {
    char ret[2] = { 0 };
    int len = 0;
    ck_assert_int_eq(base64_encode(ret, &len, sizeof(ret), decode, decode_len), COM_BUF_SMALL);
    ck_assert_int_eq(base64_decode(ret, &len, sizeof(ret), encode, encode_len), COM_BUF_SMALL);
}
END_TEST

Suite *check_base64(void)
{
    Suite *s;
    TCase *tc_core;

    s = suite_create("base64 test");

    tc_core = tcase_create("core");
    tcase_add_test(tc_core, test_ok);
    tcase_add_test(tc_core, test_input);
    tcase_add_test(tc_core, test_small_buffer);

    suite_add_tcase(s, tc_core);

    return s;
}